package com.cjzuji.rental.dao.impl;

import com.cjzuji.rental.dao.LingLOrderDao;
import com.cjzuji.rental.mapper.OrderContractMapper;
import com.cjzuji.rental.mapper.OrderMapper;
import com.cjzuji.rental.mapper.OrderUserMapper;
import com.cjzuji.rental.pojo.Orders;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;
@Service
public class LingLOrderDaoImpl implements LingLOrderDao {

    private static final Logger logger = LoggerFactory.getLogger(LingLOrderDaoImpl.class);
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    OrderUserMapper orderUserMapper;
    @Autowired
    OrderContractMapper orderContractMapper;

    @Override
    public Long save(Map<String, Object> data, Double screenRisksAmount, int screenStr , Double outBuyoutAmount) {
        System.out.println("开始添加订单"+data);
        int idCardStatus = 0;
        if (!data.get("idCard").toString().equals("")) {
            idCardStatus = 1;
        }
        int orderStatus=7;
        String deliveryNo = "";
        switch (data.get("status").toString()){
            case "06":
                orderStatus = 3;  //订单状态   3租用中
                break;
            case "20":
                orderStatus = 4; //订单状态   4逾期
                break;
            case "09":
                orderStatus = 5; //订单状态   5已完成
                break;
            default:
                orderStatus = 7;
        }
        //风控
        String opinion = "";
        switch (data.get("riskType").toString()){
            case "7":
                opinion = "A";
                break;
            case "2":
                opinion = "B";
                break;
            case "3":
                opinion = "C";
                break;
            case "4":
                opinion = "D";
                break;
            case "5":
                opinion = "E";
                break;
            default:
                opinion = " ";
        }
        if(data.get("expressNo")!=null){
            deliveryNo = data.get("expressNo").toString();
        }
        String rentStartTime = "";
        String rentLastTime = "";
        if(data.get("rentStart")!=null){
            rentStartTime = data.get("rentStart").toString();
        }
        if(data.get("unrentTime")!=null){
            rentLastTime = data.get("unrentTime").toString();
        }
        String realName = "";
        if(data.get("realName")!=null){
            realName = data.get("realName").toString();
        }
        String colour = "";
        String sku = "";
        Map<String,Object> phoneDetail = (Map<String, Object>) data.get("productColorPictures");
        if(phoneDetail!=null&&phoneDetail.get("names")!=null){
            colour = phoneDetail.get("names").toString();
            logger.info("colour {}",colour);
        }
        String spec = data.get("spec").toString();
        if (spec.contains("G")){
            sku = spec.substring(spec.lastIndexOf("/")+1);
        }
        Double surplusBondAmt = 0.0;
        if(data.get("creditDeposit")!=null){
            surplusBondAmt = Double.parseDouble(data.get("creditDeposit").toString());
        }
        String assigner =  data.containsKey("responsiblePersonName")?data.get("responsiblePersonName").toString():"";
        Orders lingLOrder = Orders.builder()
                .shopId(2)
                .platformId(4)
                .orderId(data.get("orderId").toString())
                .realName(realName)    //下单用户
                //.phone(data.get("phoneNum").toString())   //下单用户手机号
                .opinion(opinion)  //风控
                .assigner(assigner)  //分配人 备注人
                .orderIdTag(data.get("shopid").toString()) //平台订单ID
                .carBrand(data.get("productName").toString()) //产品备注
                .colour(colour)  //商品属性
                .sku(sku)  //商品SKU
                .actualFiancingAmount(Double.parseDouble(data.get("totalRentAmount").toString()))  //总租金
                .buyoutAmount(outBuyoutAmount)  //买断价
                .monthRepayment(Double.parseDouble(data.get("totalRentAmount").toString())/Double.parseDouble(data.get("totalPeriods").toString()))  //月租金
                .bondFreeCreditAmt(Double.parseDouble(data.get("totalRentAmount").toString())+outBuyoutAmount) //订单总金额
                .surplusBondAmt(surplusBondAmt) //押金
                .leaseTerm(data.get("totalPeriods").toString())   //订单租期
                .idAuthStatus(idCardStatus)  //身份证上传状态
                .rentStartTime(rentStartTime)  //租赁开始时间
                .rentLastTime(rentLastTime)  //租赁结束时间
                .status(orderStatus)    //订单状态
                .deliveryNo(deliveryNo)  //快递单号
                .createTime(data.get("placeOrderTime").toString())  // 下单时间
                .updateTime(new Date())
                .screenRisksAmount(screenRisksAmount) //碎屏金额   真实支付的碎屏价格
                .screenRisksType(screenStr)//碎屏状态
                .build();
        //System.out.println("添加订单成功~~~~~~");
        try {
            orderMapper.insert(lingLOrder);
            return lingLOrder.getId();
        }catch (Exception e){

            logger.info("添加错误" + e.getMessage());
            throw e; // 重新抛出，确保事务回滚
        }
    }

}